package com.just.spring4.ch3.aware;

import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware {
    private String beanName;
    private ResourceLoader resourceLoader;
    @Override
    public void setBeanName(String s) {
      this.beanName=s;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
       this.resourceLoader=resourceLoader;
    }

    public void outputResult(){
        System.out.println("Bean的名称为："+beanName);
        Resource resource=resourceLoader.getResource("classpath:com/just/spring4/ch3/aware/test.txt");
        try {
            System.out.println("ResourceLoader加载的内容为："+IOUtils.toString(resource.getInputStream()));
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
